<?php
/**
 * Created by PhpStorm.
 * User: lee
 *
 * 功能：退货退款申请时，系统自动帮商家确认收货
 * 版本：v1.0
 *
 *
 * 规则：1分钟拉取一次待处理数据
 *
 * 注意：可以手动，如果手动处理，则可以修复指定一个分钟内的未正确确认收货的退款单
 *      例如 php confirmReceipt.php 2018-03-20 11:19:00  修复指定时间的未正确确认收货的退款单
 *
 *
 */

ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\trade\OrderRefund as mOrderRefund;
use Biz\service\trade\OrderRefund as sOrderRefund;


if (isset($argv[1])) {
    $stopMiunte = $argv[1];
} else {
    $stopMiunte = date('Y-m-d H:i:00', time());
}

$startMiunte = date('Y-m-d H:i:00', strtotime($stopMiunte) - 60);


$where = [
    'auto_confirm_receipt_time[<>]' => [$startMiunte, $stopMiunte],
    'status'                        => mOrderRefund::STATUS_WAIT_CONFIRM_RECEIPT,
];

try {
    //取出待处理的数据
    $orderRefundList = mOrderRefund::select("*", $where);

    if (empty($orderRefundList)) {
        throw new \Exception("当前时刻没有待处理的退款单数据");
    }

    foreach ($orderRefundList as $orderRefund) {

        try {

            sOrderRefund::mgtConfirmRefund($orderRefund['shop_id'], $orderRefund['refund_order_no'], true);

        } catch (\Exception $e) {

            echo sprintf("%s    时刻:%s   refund_order_no:%s   异常：%s  \n", date('Y-m-d H:i:s'), $stopMiunte, $orderRefund['refund_order_no'], json_encode([
                'e_file' => $e->getFile(),
                'e_line' => $e->getLine(),
                'e_msg'  => $e->getMessage()
            ], JSON_UNESCAPED_UNICODE));

        } finally {
            echo sprintf("%s    时刻:%s    refund_order_no:%s   sql日志：%s   \n", date('Y-m-d H:i:s'), $stopMiunte, $orderRefund['refund_order_no'], mOrderRefund::sqlLogs());

            mOrderRefund::clearLogs();
        }
    }
} catch (\Exception $e) {

    echo sprintf("%s    %s 时刻:%s\n", date('Y-m-d H:i:s'), $e->getMessage(), $stopMiunte);

}






